package scarlett;

import java.util.Collection;
import java.util.List;

public class ScarlettSetOperator {
	
	/**
	 * Takes two items and inserts the from into the to, not allowing duplicates.
	 * @param from can be either a string or a list type
	 * @param to must be a list type
	 */
	public void Insert(Object from, Object to) {
		if(from instanceof List && to instanceof List) {
			List a = (List) from;
			List b = (List) to;
			for(Object o : a) {
				if(b.contains(o) == false) {
					b.add(o);
				}
			}
		}
		else if(from instanceof String && to instanceof List) {
			List dest = (List) to;
			String fromString = (String) from;
			if(dest.contains(fromString) == false) dest.add(fromString);
		}
	}
	
	/**
	 * Takes two items and removes all instances of the first from the second.
	 * In the case of two lists, all instances of all items in the first list will be removed from the second.
	 * @param object can be either a list or a string
	 * @param from must be a list
	 */
	public void Remove(Object object, Object from) {
		if(object instanceof String && from instanceof List) {
			List fromList = (List) from;
			if(fromList.remove(object) == false) {
				System.err.println("***ERROR*** List did not contain the object " + object.toString());
			}
		}
		else if(object instanceof List && from instanceof List) {
			((List) from).removeAll((List) object);
		}
	}
	
	/**
	 * Takes two collections and performs a union on the two, storing them in destination set.
	 * @param set1
	 * @param set2
	 * @param destSet
	 */
	public void Join(Object set1, Object set2, Object destSet) {
		if(set1 instanceof Collection && set2 instanceof Collection && destSet instanceof Collection) {
			Collection c_set1 = (Collection) set1;
			Collection c_set2 = (Collection) set2;
			Collection c_dest = (Collection) destSet;
			for(Object o : c_set1) {
				if(c_set2.contains(o)) {
					c_dest.add(o);
				}
			}
		}
	}
}
